<?php
	include_once("db/PayStation.inc");
	include_once("db/Region.inc");
	include_once("db/Ministry.inc");
	include_once("db/Department.inc");
	include_once("db/Province.inc");
	include_once("db/District.inc");
	include_once("UserView.php");
	
	if (isset($_REQUEST["payStationFilter"])) {
		$_SESSION["ministry"] = $_REQUEST["ministry"];
		$_SESSION["department"] = $_REQUEST["department"];
		$_SESSION["province"] = $_REQUEST["province"];
		$_SESSION["district"] = $_REQUEST["district"];	
	}
	
	$ministry = null;
	$department = null;
	$province = null;
	$district = null;
	
	if (isset($_SESSION["ministry"])) 
		$ministry = $_SESSION["ministry"];
		
	if (isset($_SESSION["department"]))
		$department = $_SESSION["department"];
		
	if (isset($_SESSION["province"]))
		$province = $_SESSION["province"];
	
	if (isset($_SESSION["district"]))
		$district = $_SESSION["district"];
?>

<h1>Pay Stations</h1>

<a href="?action=newPayStation&province=0">Add Pay Station</a><br/>

<script language="javascript"><!--
	function submitFilter() {
		document.getElementById("payStationFilter").submit();
	}
	
	function setListValue(list, src) {
		document.getElementById(list).value = document.getElementById(src).value;
		submitFilter();
	}
--></script>

<form method="get" id="payStationFilter" action=".">
	<input type="hidden" name="payStationFilter" value="true" />
	<input type="hidden" name="action" value="viewPayStations" />
	<table cellspacing="0" class="formLayout">
		<tr>
			<td>Ministry</td>
			<td>
				<input type="text" maxlength="4" size="6" id="ministryText" onChange="setListValue('ministry', 'ministryText')" />
				<select id="ministry" name="ministry" onChange="submitFilter()">
				
				<?php
					$mins = Ministry::getAllMinistries();
					
					foreach ($mins as $m) {
						print "<option value=\"$m->id\" ";
						if ($m->id == $ministry) print "selected=\"selected\" ";
						print ">$m->id - $m->description";						
					}
				?>
				</select>
			</td>
		</tr>
		<tr>
			<td>Department</td>
			<td>
				<input type="text" maxlength="4" size="6" id="departmentText"
						onChange="setListValue('department', 'departmentText')" />
				<select id="department" name="department" onChange="submitFilter()" >
				
				<?php
					$deps = Department::getAllDepartments();
					
					foreach ($deps as $d) {
						print "<option value=\"$d->id\" ";
						if ($d->id == $department) print "selected=\"selected\" ";
						print ">$d->id - $d->description</option>";
 					}
 				?>
				</select>
			</td>
		</tr>
		<tr>
			<td>Province</td>
			<td>
				<input type="text" maxlength="4" size="6" id="provinceText"
						onChange="setListValue('province', 'provinceText')" />
				<select id="province" name="province" onChange="submitFilter()">
					<option></option>
					<?php
						$provs = Province::getAllProvinces();
						
						foreach ($provs as $p) {
							print "<option value=\"$p->id\" ";
							if ($p->id == $province) print "selected=\"selected\" ";
							print ">$p->id - $p->name</option>";
						}
					?>
				</select>
			</td>
		</tr>
		<tr>
			<td>District</td>
			<td>
				<input type="text" maxlength="4" size="6" id="districtText"
						onChange="setListValue('district', 'districtText')" />
				<select id="district" name="district" onChange="submitFilter()">
					<option></option>
					<?php
						if ($province != "") {
							$dist = District::getAllDistricts($province, $district);
							
							foreach ($dist as $d) {
								print "<option value=\"$d->id\" ";
								if ($d->id == $district) print "selected=\"selected\" ";
								print ">$d->id - $d->name</option>";
							}
						}
					?>
				</select>
			</td>
		</tr>
	</td>
</form>

<table cellspacing="0">
	<tr class="first">
		<td>&nbsp;</td>
		<td>Code</td>
		<td>Ministry</td>
		<td>Department</td>
		<td>Description</td>
		<td>Region</td>
		<td>Province</td>
		<td>District</td>
	</tr>
	
<?php
	$ps = array();
	if ($ministry != null && $department != null)
		$ps = PayStation::getAllPayStations($ministry, $department);
	
	foreach ($ps as $p) {
		$refMinistry = Ministry::getMinistry($p->ministryId);
		$refDept = Department::getDepartment($p->deptCode);
		$refRegion = Region::getRegion($p->regionCode);
		$refProvince = Province::getProvince($p->provinceCode);
		$refDistrict = District::getDistrict($p->districtCode);
		
		
		if ($province == "" || $p->provinceCode == $province &&
				$district == "" || $p->districtCode == $district) {
?>

	<tr>
		<td>&nbsp;<a href="?action=editPayStation&ministry=<?php print $p->ministryId ?>&department=<?php print $p->deptCode ?>&id=<?php print ltrim($p->payStationCode, '0') ?>&province=<?php print $p->provinceCode ?>">Edit</a>&nbsp;</td>
		<td><?php print $p->payStationCode ?></td>
		<td><?php print $refMinistry->description ?></td>
		<td><?php print $refDept->description ?></td>
		<td><?php print $p->description ?></td>
		<td><?php print $refRegion->name ?></td>
		<td><?php print $refProvince->name ?></td>
		<td><?php print $refDistrict->name ?></td>
	</tr>
	
<?php
		}			
	}
?>

</table>